﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebTV_Kickstarter;

namespace Kickstarter.Controllers
{
    public class RatingController : Controller
    {
        private EFDataModelContainer db = new EFDataModelContainer();
        
        // GET: /Rating/
        public ActionResult Index()
        {
            var ratingset = db.RatingSet.Include(r => r.User).Include(r => r.Project);
            return View(ratingset.ToList());
        }

        public ActionResult Thankyou(int? id)
        {
            ViewBag.ProjectID = id;
            return View();
        }

        public ActionResult Beoordeel(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            if (Session["Gebruiker"] != null)
            {
                Project project = Project.FindProject(id);
                Gebruiker gebruiker = (Gebruiker)Session["Gebruiker"];

                if (project == null)
                {
                    return HttpNotFound();
                }

                ViewBag.ProjectName = Project.FindProject(id).Title;
                ViewBag.ProjectID = Project.FindProject(id).Id;

                Rating rating = new Rating();
                rating.ProjectId = (int)id;
                rating.UserId = gebruiker.Id;
                rating.Project = project;
                rating.User = gebruiker;
                return View(rating);
            
            }
            else
            {
                Session["redirectBack"] = "../Rating/Beoordeel/" + id;
                return RedirectToAction("../Gebruiker/Login/");
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Beoordeel([Bind(Include = "Id,Score,UserId,ProjectId")] Rating rating)
        {
            if (ModelState.IsValid)
            {
                Rating.GiveRating(rating);

                return RedirectToAction("Thankyou/" + rating.ProjectId);
            }

            //ViewBag.UserId = new SelectList(db.GebruikerSet, "Id", "Name", rating.UserId);
            //ViewBag.ProjectId = new SelectList(db.ProjectSet, "Id", "Title", rating.ProjectId);
            return View(rating);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
